package com.hs.sfzy.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hs.sfzy.entity.User;
import com.hs.sfzy.service.UserService;
import com.hs.sfzy.util.Result;
import com.hs.sfzy.util.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @BelongsProject: sfzyBackEnd
 * @BelongsPackage: com.hs.sfzy.controller
 * @ClassName UserController
 * @Author: 陈千鱼不是鱼
 * @CreateTime: 2024-10-24  19:26
 * @Description: 普通用户控制层
 * @Version: 1.0
 */
@RestController
@RequestMapping("/user")
@Api(tags = "普通用户端")
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;

    @GetMapping("/getIdAndUsername")
    @ApiOperation("获取普通用户id与用户名")
    public Result getIdAndUsername() {
        return Result.success(userService.getIdAndUsername());
    }

    /**
     * 根据id获取用户信息
     *
     * @param id
     * @return java.lang.Object
     * @author QinKunPeng
     * @date 2024/10/24 20:30
     */
    @GetMapping("/{id}")
    public Object getUserById(@PathVariable("id") Long id) {
        User user = userService.getUserById(id);
        return ResultUtil.success("获取用户成功").setData(user);
    }

    @PostMapping("/add")
    public Result add(@RequestBody User user) {
        int num = userService.insert(user);
        if (num != 0) {
            return Result.success("添加成功");
        } else {
            return Result.error("添加失败");
        }
    }

    @PutMapping("/edit")
    public Result update(@RequestBody User user) {
        int num = userService.update(user);
        if (num != 0) {
            return Result.success("修改成功");
        } else {
            return Result.error("修改失败");
        }
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") long id) {
        try {
            int num = userService.delete(id);
            if (num != 0) {
                return Result.success("删除成功");
            } else {
                return Result.error("删除失败");
            }
        } catch (Exception e) {
            return Result.error("删除失败");
        }
    }

    @GetMapping("/findAll")
    public Result selectAll(User user) {
        List<User> users = userService.selectAll(user);
        return Result.success(users);
    }

    @GetMapping("/findByPage")
    public Result selectByPage(
            @RequestParam(required = false, defaultValue = "1") int pageNum,
            @RequestParam(required = false, defaultValue = "5") int pageSize,
            User user) {
        PageInfo<User> info = userService.pageInfo(pageNum, pageSize, user);
        return Result.success(info);
    }

    @PostMapping("/login")
    public Result login(@RequestBody User user) {
        User login = userService.login(user.getUsername(), user.getPassword());
        if (login == null) {
            return Result.error("用户名或密码有误！");
        }
        return Result.success(login);
    }

    @GetMapping("/forwx/{id}")
    public Object getUserCourseById(
            @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
            @RequestParam(value = "pageSize", defaultValue = "5") int pageSize,
            @PathVariable(required = false) Long id) {
        if (id == null) {
            return ResultUtil.fail("id有误");
        } else {
            Object userCourseById = userService.getUserCourseById(pageNum, pageSize, id);
            return ResultUtil.success().setData(userCourseById);
        }
    }


}
